//void get_text_from_file(HWND hwnd);
//BOOL LoadTextFileToEdit(HWND hEdit, LPCTSTR pszFileName);
void get_text_from_file(HWND hwnd,long long int *caractere_text);
BOOL LoadTextFileToEdit(HWND hEdit, LPCTSTR pszFileName,long long int *caractere_text);

bool random_main(HWND hwnd,int nr_operatii,int *nr_ins,int *nr_sterg,int *tot_ins,int *tot_sterg)
{
	long long int caractere_text;
	//-------------------
	get_text_from_file(hwnd,&caractere_text);
	if(buffer_text==NULL)
		return 0;
	timp_gap=0; timp_liste=0;
	MessageBox(hwnd,"Va rugam asteptati Va dura in functie\nde numarul de operatii","Mesaj",MB_OK);
	if(TEXT_EDITOR.buffer)
	{
		free(TEXT_EDITOR.buffer);
		TEXT_EDITOR.buffer=NULL;
	}
	*tot_ins+=caractere_text;
	(*nr_ins)++;
//------------------------------------------
//reinitializarea structurii
	TEXT_EDITOR.DIM=10;
	TEXT_EDITOR.dim_begin=0;
	TEXT_EDITOR.dim_end=0;
	TEXT_EDITOR.dim_gap=10;
	TEXT_EDITOR.buffer=(char*)malloc(sizeof(char)*10);
	TEXT_EDITOR.head_gap=TEXT_EDITOR.buffer;
	TEXT_EDITOR.after_gap=TEXT_EDITOR.buffer+9;
//--------------------------------------------
	long long int i,j,poz=0,poz2;
	LARGE_INTEGER tickPerSecond;
	LARGE_INTEGER t1,t2; 	
	list *linie;
	prim=NULL,ultim=NULL;
	_int64 t_gap=0,t_liste=0;		// memoram timplu total
	timp_gap=0; timp_liste=0;
//-----------------------------------------------------------
			// se masoara timpul pentru inserarea din fisier
	QueryPerformanceCounter(&t1);			//  gap_bufferul
	for(i=0;i<caractere_text;i++)
	{
		if(caractere_text==i)
			break;
		if(buffer_text[i+1])
			if(buffer_text[i+1]=='\n')
				i++;
			insert_char(&TEXT_EDITOR,buffer_text[i],poz);
			poz++;
	}
	QueryPerformanceCounter(&t2);
	t_gap+=t2.QuadPart-t1.QuadPart;

	poz=0;
	QueryPerformanceCounter(&t1);
	linie=search_linie(prim,&poz);
	for(i=0;i<caractere_text;i++)
	{
		if(caractere_text==i)
			break;
		if(buffer_text[i+1])
			if(buffer_text[i+1]=='\n')
				i++;
			inserare_linked(buffer_text[i],&poz,&prim,&ultim,&linie);
	}
	QueryPerformanceCounter(&t2);
	t_liste+=t2.QuadPart-t1.QuadPart;

//////-------------------------------------------------------------------
	bool ok=0,ok2=0;
	long long int aux,nr_caractere;
	char chr;
	
	for(i=0;i<nr_operatii;i++)			//cate operatii cere utilizatorul
	{
		poz= rand()*rand() % (TEXT_EDITOR.dim_begin + TEXT_EDITOR.dim_end);			//se alege o pozitie random care sa fie in buffer
		ok=0;ok2=0;
		while(!ok2)			//se alege o obtiune: 1=adaugare, 0=stergere
		{
			aux=rand()%10;
			if(aux<2)
				ok2=1;
		}
		ok=aux;
		if(poz==0)
			poz++;
		switch(ok)
		{
			case 1:			// adica adaugare
				(*nr_ins)++;		// crestem nr de inserari
				ok=0;
				while(!ok)							// generare random a unui numar de caractere ce trebuie introdus
				{
					nr_caractere=rand() % 200;
					if(nr_caractere>0)
						ok=1;
				}
				(*tot_ins)+=nr_caractere;//crestem nr de caractere inserate
				free(buffer_text);
				buffer_text=NULL;
				buffer_text=(char *)malloc(sizeof(char)*(nr_caractere+1));

				for(j=0;j<nr_caractere;j++)
				{
					ok=0;
					while(!ok)
					{
						aux=rand()*rand() % 100;		// se aleg aleator caractere ce au codurile ascii intre 32 si 126
						if(aux>=0 && aux<=94)
							ok=1;
					}
					chr=aux+32;
					buffer_text[j]=chr;
				}
				buffer_text[j]=NULL;
				poz2=poz;
				// se masoara timpul pentru inserarea
				QueryPerformanceCounter(&t1);			//  gap_bufferul
				for(j=0;j<nr_caractere;j++)
				{
					if(buffer_text[j+1])
						if(buffer_text[j+1]=='\n')
							j++;
						insert_char(&TEXT_EDITOR,buffer_text[j],poz2);
						poz2++;
				}
				QueryPerformanceCounter(&t2);
				t_gap+=t2.QuadPart-t1.QuadPart;

				poz2=poz;
				QueryPerformanceCounter(&t1);		// se masoara timpul inserarii liste
				linie=search_linie(prim,&poz2);
				for(j=0;j<nr_caractere;j++)
				{
					if(buffer_text[j+1])
						if(buffer_text[j+1]=='\n')
							j++;
						inserare_linked(buffer_text[j],&poz2,&prim,&ultim,&linie);
				}
				QueryPerformanceCounter(&t2);
				t_liste+=t2.QuadPart-t1.QuadPart;
				break;
			case 0:			//adica stergere
				if(TEXT_EDITOR.dim_begin+TEXT_EDITOR.dim_end<=0)
					break;
				(*nr_sterg)++;		// se incrementeaza numarul de stergeri
				nr_caractere=rand()*rand() % 500;    // se genereaza un numar de carectere ce trebuie sters mai mic decat 500
				if(nr_caractere>(TEXT_EDITOR.dim_begin+TEXT_EDITOR.dim_end-poz))		//daca se genereaza un numar de caractere mai mare
						nr_caractere=TEXT_EDITOR.dim_begin+TEXT_EDITOR.dim_end-poz;		// decat are bufferul de la pozitie pana la sfarsit
				(*tot_sterg)+=nr_caractere;		// se creste nr de caractere sterse
				QueryPerformanceCounter(&t1);
				for(j=0;j<nr_caractere;j++)
				{
						stergere(&TEXT_EDITOR,poz);	
				}
				QueryPerformanceCounter(&t2);
				t_gap+=t2.QuadPart-t1.QuadPart;
				QueryPerformanceCounter(&t1);
					linie=search_linie(prim,&poz);
					stergere(&linie,poz,nr_caractere,&prim);
				QueryPerformanceCounter(&t2);
				t_liste+=t2.QuadPart-t1.QuadPart;
				
				break;
		}
	}
	
	QueryPerformanceFrequency(&tickPerSecond);
	timp_gap=(double)t_gap/(1000*((double)tickPerSecond.QuadPart/1000));
	timp_liste=(double)t_liste/(1000*((double)tickPerSecond.QuadPart/1000));
//	construire_vectori();
	int r,rr;
	if(superbuff!=NULL)
		free(superbuff);
	superbuff=(char *)malloc(sizeof(char)*(TEXT_EDITOR.dim_begin+TEXT_EDITOR.dim_end+1));
	if(superbuff==NULL)
	{
		MessageBox(hwnd,"Eroare la alocarea dinamica functia random","erroare",MB_ICONERROR);
		return false;
	}
	for(r=0;r<TEXT_EDITOR.dim_begin;r++)
		superbuff[r]=TEXT_EDITOR.buffer[r];
	for(rr=0;rr<TEXT_EDITOR.dim_end;rr++)
		superbuff[r++]=*(TEXT_EDITOR.after_gap+rr);
	superbuff[r]='\0';
	free(buffer_text);
	buffer_text=NULL;
	if(*(&(TEXT_EDITOR.buffer))!=NULL)
	{
		free(TEXT_EDITOR.buffer);
		TEXT_EDITOR.buffer=NULL;
	}
	if(*(&(prim))!=NULL)
	{
		elibmem(&prim);
		prim=ultim=NULL;
	}
	return 1;
}

void get_text_from_file(HWND hwnd,long long int *caractere_text)
{
	OPENFILENAME ofn;
	char file_name[MAX_PATH] = "";
	ZeroMemory(&ofn, sizeof(ofn));
	ofn.lStructSize = sizeof(ofn); 
	ofn.hwndOwner = hwnd;
	ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
	ofn.lpstrFile = file_name;
	ofn.nMaxFile = MAX_PATH;
	ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
	ofn.lpstrDefExt = "txt";
	GetOpenFileName(&ofn);
	LoadTextFileToEdit(hwnd,file_name,caractere_text);	
}

BOOL LoadTextFileToEdit(HWND hEdit, LPCTSTR pszFileName,long long int *caractere_text)
{
	HANDLE hFile;
	BOOL bSuccess = FALSE;
	hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
	OPEN_EXISTING, 0, NULL);
	if(hFile != INVALID_HANDLE_VALUE)
	{
		DWORD dwFileSize;
		dwFileSize = GetFileSize(hFile, NULL);
		if(dwFileSize != 0xFFFFFFFF)
		{
	//		buffer_text = (LPSTR) GlobalAlloc(GPTR, dwFileSize + 1);
			(*caractere_text)=dwFileSize;
			buffer_text=(char *)malloc(sizeof(char)*(dwFileSize + 1));
			if(buffer_text != NULL)
			{
				DWORD dwRead;
				if(ReadFile(hFile, buffer_text, dwFileSize , &dwRead, NULL))
				{
					buffer_text[dwFileSize] = NULL; 
					if(buffer_text)
						bSuccess = TRUE; 
				}
			}
		}
		CloseHandle(hFile);
	}
	return bSuccess;
}